SHELL=/bin/sh
CPP=g++
LINKER=g++
CPPFLAGS=--std=c++0x -fPIC
LINKERFLAGS=
LIBNAME=icecode-config
LIBFILE=lib$(LIBNAME).so
PKGCONFIGFILE=$(LIBNAME).pc
OBJECTS=Configuration.o
DESTDIR=/usr/local
includedir=/include
libdir=/lib
projdir=/icecode/config

.PHONY: clean install uninstall

all: $(LIBFILE) $(PKGCONFIGFILE)

$(LIBFILE): $(OBJECTS)
	$(LINKER) -shared -o $(LIBFILE) $(OBJECTS) $(LINKERFLAGS)
$(OBJECTS): %.o : %.cpp
	$(CPP) -c -o $@ $(CPPFLAGS) $<

clean:
	$(RM) $(OBJECTS) $(LIBFILE) $(PKGCONFIGFILE)

install:
# Directories
	install -m 755 -d $(DESTDIR)$(includedir)$(projdir)
	install -m 755 -d $(DESTDIR)$(libdir)/pkgconfig

# Includes
	install -m 644 config.hpp $(DESTDIR)$(includedir)$(projdir)/

# Libraries
	install -m 755 $(LIBFILE) $(DESTDIR)$(libdir)/

# pkg-config files
	install -m 644 $(PKGCONFIGFILE) $(DESTDIR)$(libdir)/pkgconfig/

# DL run-time bindings
	ldconfig -l $(DESTDIR)$(libdir)/$(LIBFILE)

uninstall:
# Libraries
	$(RM) $(DESTDIR)$(libdir)/$(LIBFILE)

# Includes
	$(RM) $(DESTDIR)$(includedir)$(projdir)/config.hpp

# Directories
	rmdir -p --ignore-fail-on-non-empty $(DESTDIR)$(includedir)$(projdir)

# pkg-config files
	$(RM) $(DESTDIR)$(libdir)/pkgconfig/$(PKGCONFIGFILE)

# DL run-time bindings
	ldconfig

$(PKGCONFIGFILE): pkg-config.pc.in
	sed -e "s:@@DESTDIR@@:$(DESTDIR):g" -e "s:@@INCLUDEDIR@@:$(includedir):g" -e "s:@@LIBDIR@@:$(libdir):g" -e "s:@@LIBNAME@@:$(LIBNAME):g" pkg-config.pc.in > $(PKGCONFIGFILE)
